package com.taotao.order.service.impl;

import com.taotao.common.*;
import com.taotao.mapper.TbOrderItemMapper;
import com.taotao.mapper.TbOrderMapper;
import com.taotao.mapper.TbOrderShippingMapper;
import com.taotao.order.pojo.Order;
import com.taotao.order.pojo.OrderResult;
import com.taotao.order.service.OrderService;
import com.taotao.pojo.TbOrder;
import com.taotao.pojo.TbOrderItem;
import com.taotao.pojo.TbOrderShipping;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private TbOrderShippingMapper tbOrderShippingMapper;

    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;

    @Transactional
    @Override
    public TaotaoResult createOrder(String orderJson) {
        if (StringUtils.isBlank(orderJson)) {
            return TaotaoResult.error("创建订单失败:订单信息不能为空!");
        }
        try {
            Order order = JsonUtils.jsonToPojo(orderJson, Order.class);
            //1.写tb_order表
            //2.写tb_order_item表
            //3.写tb_order_shipping表
            //4.组装返回结果
            OrderResult orderResult = saveOrder2DB(order);
            return TaotaoResult.ok(orderResult);
        } catch (Exception e) {
            log.error("转换订单信息异常", e);
            return TaotaoResult.error("转换订单信息异常", ExceptionUtil.getStackTrace(e));
        }
    }

    /**
     * 生成订单信息,保存到数据库
     *
     * @param order 返回portal需要的订单信息
     * @return
     */
    private OrderResult saveOrder2DB(Order order) {
        OrderResult orderResult = new OrderResult();

        //生成订单ID
        String orderId = IDUtils.genImageNameByUUID();

        //保存订单表tb_order
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOrderId(orderId);
        tbOrder.setUserId(order.getTbUser().getId());
        tbOrder.setPaymentType(order.getPaymentType());
        tbOrder.setPayment(order.getPayment() + "");
        tbOrder.setStatus(SystemConstants.TAOTAO_ORDER_STATUS_NOPAY);
        tbOrder.setCreateTime(new Date());
        tbOrder.setShippingName("顺丰");
        tbOrder.setShippingCode("2168632916987");
        tbOrder.setBuyerNick(order.getTbUser().getUsername());
        tbOrder.setBuyerMessage("早点送达");
        tbOrderMapper.insert(tbOrder);

        //保存订单商品tb_order_item
        List<TbOrderItem> tbOrderItemList = order.getOrderItems();
        for (TbOrderItem tbOrderItem : tbOrderItemList) {
            String orderItemId = IDUtils.genImageNameByUUID();
            tbOrderItem.setId(orderItemId);
            tbOrderItem.setItemId(tbOrderItem.getItemId());
            tbOrderItem.setOrderId(orderId);
            tbOrderItemMapper.insertSelective(tbOrderItem);
        }

        //保存物流信息
        TbOrderShipping orderShipping = order.getOrderShipping();
        orderShipping.setOrderId(orderId);
        tbOrderShippingMapper.insert(orderShipping);

        //设置创建订单后返回的结果
        orderResult.setOrderId(orderId);
        String date = new DateTime().plusDays(2).toString("yyyy-MM-dd");
        orderResult.setDate(date);
        orderResult.setTotalPayment(order.getPayment());

        return orderResult;
    }
}
